- 
                Notifications
    You must be signed in to change notification settings 
- Fork 16
Show torrent fields: creation_date, created_by, encoding #297
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Show torrent fields: creation_date, created_by, encoding #297
Conversation
f5efbd9    to
    fe4e157      
    Compare
  
    fe4e157    to
    81fa3bb      
    Compare
  
    81fa3bb    to
    d38abfc      
    Compare
  
    | @josecelano Rebased and CI OK | 
| } | ||
| }); | ||
|  | ||
| // Takes the date in milliseconds/Epoch Time and converts it to human readable format | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @mario-nt,
- I would move the comment to the function. If you think the name is not enough, you could change the name to epochTimeToHumanReadableUTCortimestampToHumanReadableUTC
- Timestamp is in seconds (props.torrent.creation_date), not milliseconds. The Date constructor accepts milliseconds (new Date(date * 1000);). We are passing seconds to the function.
        
          
                src/helpers/DateConverter.ts
              
                Outdated
          
        
      | export function epochToUtc (date: number) { | ||
| const convertedDate = new Date(date * 1000); | ||
| return convertedDate.toDateString(); | ||
| } | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @mario-nt since we are using TypeScript maybe we can introduce a new Timestamp type to make it explicit what the function needs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was using snake_case for JS functions and I guess also for filenames because they all start with a lowercase:
- sanitizer.ts
- slug.ts
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be good to add a unit test for the function but it's a pain to add unit tests with Nuxt.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @mario-nt since we are using TypeScript maybe we can introduce a new
Timestamptype to make it explicit what the function needs.
Maybe something like:
type Timestamp = number;
export function epochToUtc (timestamp: Timestamp) {
  new Date(timestamp.toMilliseconds()).toDateString();
}But you can even use a field and check that the number is an integer.
If the torrent creation date field is present in the torrent file, it is now shown as a UTC date in a human readable format. If there is no creation date, a no creation date provided message is displayed. And if there is an error parsing the creation date or if it is a no valid date, an invalid date text is shown.
003c80d    to
    f4b5ca6      
    Compare
  
    Error checks added to the function that converts the torrent creation date from seconds to UTC human readable format. Refactor to the function logic's code has been applied.
f4b5ca6    to
    ad0c609      
    Compare
  
    If the torrent creation date field is present in the torrent file, it is now shown as a UTC date in a human readable format. If there is no creation date, a no creation date provided message is displayed. And if there is an error parsing the creation date or if it is a no valid date, an invalid date text is shown.
Error checks added to the function that converts the torrent creation date from seconds to UTC human readable format. Refactor to the function logic's code has been applied.
ad0c609    to
    89f3f0c      
    Compare
  
    Date converter helper function now throws errors if the timestamp passed as argument is not an integer and if the Date() constructor returns an invalid date.
If the torrent creation date field is present in the torrent file, it is now shown as a UTC date in a human readable format. If there is no creation date, a no creation date provided message is displayed. And if there is an error parsing the creation date or if it is a no valid date, an invalid date text is shown.
Error checks added to the function that converts the torrent creation date from seconds to UTC human readable format. Refactor to the function logic's code has been applied.
Date converter helper function now throws errors if the timestamp passed as argument is not an integer and if the Date() constructor returns an invalid date.
1443c0e    to
    f6ff259      
    Compare
  
    | @josecelano Ready for review. | 
| @josecelano Ready for review. | 
| <template v-if="!collapsed"> | ||
| <div class="flex flex-col w-full h-full p-6 grow bg-base-100 rounded-2xl"> | ||
| <template v-if="torrent.creation_date"> | ||
| {{ unixTimeToHumanReadableUTC(torrent.creation_date) }} | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @mario-nt maybe inside the components we could use a wrapper helper with the name formatDateFromTimestamp.
        
          
                src/helpers/DateConverter.ts
              
                Outdated
          
        
      | class InvalidDateError extends Error {} | ||
| class WrongTimestamp extends Error {} | ||
|  | ||
| // Takes the date in seconds from Epoch time and converts it to human readable format. | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @mario-nt I think we should use a format for comments like https://tsdoc.org/ but we can enforce that after installing MegaLinter.
        
          
                src/helpers/DateConverter.ts
              
                Outdated
          
        
      | try { | ||
| milliseconds = creationDate * 1000; | ||
| } catch (error) { | ||
| return new WrongTimestamp(`Could not convert ${creationDate} to milliseconds`); | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @mario-nt I think this line is never reached. If I'm not wrong miliseconds would be Infinity but ti will not throw an exception.
// Online Javascript Editor for free
// Write, Edit and Run your Javascript code using JS Online Compiler
let seconds = Number.MAX_VALUE;
let miliseconds = seconds * 1000;
console.log("test1: ", miliseconds); // Infinity
try {
  milliseconds = seconds * 1000;
} catch (error) {
  console.log("error", error);
}
console.log("test2: ", miliseconds); // Infinity        
          
                src/helpers/DateConverter.ts
              
                Outdated
          
        
      | } catch (error) { | ||
| return new InvalidDateError(`Could not create a new date from ${milliseconds}`); | ||
| } | ||
| return !isNaN(convertedDate.valueOf()) ? convertedDate.toDateString() : new InvalidDateError(`Could not create a valid date from ${milliseconds}`); | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @mario-nt In general I prefer positive conditional:
  return isNaN(convertedDate.valueOf())
    ? new InvalidDateError(
        `Could not create a valid date from ${milliseconds}`
      )
    : convertedDate.toDateString();See https://softwareengineering.stackexchange.com/a/350474/430081
        
          
                src/helpers/DateConverter.ts
              
                Outdated
          
        
      | try { | ||
| convertedDate = new Date(milliseconds); | ||
| } catch (error) { | ||
| return new InvalidDateError(`Could not create a new date from ${milliseconds}`); | ||
| } | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @mario-nt I think the <Date function does not thorw any exception:
let seconds = Number.MAX_VALUE;
let miliseconds = seconds * 1000;
console.log("test1: ", miliseconds); // Infinity
try {
  milliseconds = seconds * 1000;
} catch (error) {
  console.log("overflow error", error);
}
console.log("test2: ", miliseconds); // Infinity
try {
  convertedDate = new Date(seconds * 1000);
} catch (error) {
  console.log("Date error", error);
}
console.log("test3: ", convertedDate); // InfinityThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some tests I did:
let seconds = Number.MAX_VALUE;
let miliseconds = seconds * 1000;
console.log("test1: ", miliseconds); // Infinity
try {
  milliseconds = seconds * 1000;
} catch (error) {
  console.log("overflow error", error);
}
console.log("test2: ", miliseconds); // Infinity
try {
  convertedDate = new Date(seconds * 1000);
} catch (error) {
  console.log("Date error", error);
}
console.log("test3: ", convertedDate); // Invalid Date
convertedDate = new Date(1701688171 * 1000);
console.log("test4: ", convertedDate);
console.log("test5: ", convertedDate.toISOString());
console.log("test6: ", convertedDate.toUTCString());
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @mario-nt I've just created another example.
Why do you want to differentiate between an invalid timestamp in the database and a timestamp greater than the maximum Date? Now we are showing the value causing the error so we can find out later what happened.
| @josecelano Changes applied. | 
| 
 Hi @mario-nt it looks good but it needs to be rebased. | 
…ding
    Torrent details page now displays the creation date, created by and encoding fields.
    If the torrent creation date field is present in the torrent file, it is now shown as a UTC date in a human readable format. If there is no creation date, a no creation date provided message is displayed. And if there is an error parsing the creation date or if it is a no valid date, an invalid date text is shown.
Error checks added to the function that converts the torrent creation date from seconds to UTC human readable format. Refactor to the function logic's code has been applied.
Date converter helper function now throws errors if the timestamp passed as argument is not an integer and if the Date() constructor returns an invalid date.
85a927c    to
    4281526      
    Compare
  
    | @josecelano Rebased. | 
| ACK 4281526 | 

Resolves #278.